


use "C:\Users\Administrador\Meu Drive\00 Pesquisa\# Gender Gap Turnout\Dados e Análise\TSE Data\base_final.dta", clear








*------------------------------------------------------------*
* Generate gender turnout gap variables (%)
* Formula: 100 * ((female turnout - male turnout) / (female + male eligible))
*------------------------------------------------------------*

gen pturnout = 100 * (comparecimento_total)/(aptos_total )
label var pturnout      "Turnout (%)"

gen fem_turnout = 100 * (comparecimento_total_mulheres)/(aptos_total )
label var fem_turnout    "Female Turnout (%)"

gen mal_turnout = 100 * (comparecimento_total_homens)/(aptos_total )
label var mal_turnout  "Male Turnout (%)"




* ---- Female - voluntary age Turnout ----
gen fem_turnout_16 = 100 * (comparecimento_mulheres_16) / (aptos_mulheres_16)
label var fem_turnout_16 "Female Turnout 16 (%)"

gen fem_turnout_17 = 100 * (comparecimento_mulheres_17) / (aptos_mulheres_17)
label var fem_turnout_17 "Female Turnout 17 (%)"

gen fem_turnout_18 = 100 * (comparecimento_mulheres_18) / (aptos_mulheres_18)
label var fem_turnout_18 "Female Turnout 18 (%)"

gen fem_turnout_19 = 100 * (comparecimento_mulheres_19) / (aptos_mulheres_19)
label var fem_turnout_19 "Female Turnout 19 (%)"

* ---- Male voluntary age  Turnout ----
gen mal_turnout_16 = 100 * (comparecimento_homens_16) / (aptos_homens_16)
label var mal_turnout_16 "Male Turnout 16 (%)"

gen mal_turnout_17 = 100 * (comparecimento_homens_17) / (aptos_homens_17)
label var mal_turnout_17 "Male Turnout 17 (%)"

gen mal_turnout_18 = 100 * (comparecimento_homens_18) / (aptos_homens_18)
label var mal_turnout_18 "Male Turnout 18 (%)"

gen mal_turnout_19 = 100 * (comparecimento_homens_19) / (aptos_homens_19)
label var mal_turnout_19 "Male Turnout 19 (%)"






gen ggap_16      = 100 * ((comparecimento_mulheres_16      - comparecimento_homens_16)      / (aptos_mulheres_16      + aptos_homens_16))
gen ggap_17      = 100 * ((comparecimento_mulheres_17      - comparecimento_homens_17)      / (aptos_mulheres_17      + aptos_homens_17))
gen ggap_18      = 100 * ((comparecimento_mulheres_18      - comparecimento_homens_18)      / (aptos_mulheres_18      + aptos_homens_18))
gen ggap_19      = 100 * ((comparecimento_mulheres_19      - comparecimento_homens_19)      / (aptos_mulheres_19      + aptos_homens_19))
gen ggap_20      = 100 * ((comparecimento_mulheres_20      - comparecimento_homens_20)      / (aptos_mulheres_20      + aptos_homens_20))
gen ggap_21_24   = 100 * ((comparecimento_mulheres_21_24   - comparecimento_homens_21_24)   / (aptos_mulheres_21_24   + aptos_homens_21_24))
gen ggap_25_29   = 100 * ((comparecimento_mulheres_25_29   - comparecimento_homens_25_29)   / (aptos_mulheres_25_29   + aptos_homens_25_29))
gen ggap_30_34   = 100 * ((comparecimento_mulheres_30_34   - comparecimento_homens_30_34)   / (aptos_mulheres_30_34   + aptos_homens_30_34))
gen ggap_35_39   = 100 * ((comparecimento_mulheres_35_39   - comparecimento_homens_35_39)   / (aptos_mulheres_35_39   + aptos_homens_35_39))
gen ggap_40_44   = 100 * ((comparecimento_mulheres_40_44   - comparecimento_homens_40_44)   / (aptos_mulheres_40_44   + aptos_homens_40_44))
gen ggap_45_49   = 100 * ((comparecimento_mulheres_45_49   - comparecimento_homens_45_49)   / (aptos_mulheres_45_49   + aptos_homens_45_49))
gen ggap_50_54   = 100 * ((comparecimento_mulheres_50_54   - comparecimento_homens_50_54)   / (aptos_mulheres_50_54   + aptos_homens_50_54))
gen ggap_55_59   = 100 * ((comparecimento_mulheres_55_59   - comparecimento_homens_55_59)   / (aptos_mulheres_55_59   + aptos_homens_55_59))
gen ggap_60_64   = 100 * ((comparecimento_mulheres_60_64   - comparecimento_homens_60_64)   / (aptos_mulheres_60_64   + aptos_homens_60_64))
gen ggap_65_69   = 100 * ((comparecimento_mulheres_65_69   - comparecimento_homens_65_69)   / (aptos_mulheres_65_69   + aptos_homens_65_69))
gen ggap_70_74   = 100 * ((comparecimento_mulheres_70_74   - comparecimento_homens_70_74)   / (aptos_mulheres_70_74   + aptos_homens_70_74))
gen ggap_75_79   = 100 * ((comparecimento_mulheres_75_79   - comparecimento_homens_75_79)   / (aptos_mulheres_75_79   + aptos_homens_75_79))
gen ggap_80_84   = 100 * ((comparecimento_mulheres_80_84   - comparecimento_homens_80_84)   / (aptos_mulheres_80_84   + aptos_homens_80_84))
gen ggap_85_89   = 100 * ((comparecimento_mulheres_85_89   - comparecimento_homens_85_89)   / (aptos_mulheres_85_89   + aptos_homens_85_89))
gen ggap_90_94   = 100 * ((comparecimento_mulheres_90_94   - comparecimento_homens_90_94)   / (aptos_mulheres_90_94   + aptos_homens_90_94))
gen ggap_95_99   = 100 * ((comparecimento_mulheres_95_99   - comparecimento_homens_95_99)   / (aptos_mulheres_95_99   + aptos_homens_95_99))
gen ggap_total   = 100 * ((comparecimento_total_mulheres   - comparecimento_total_homens)   / (aptos_total_mulheres   + aptos_total_homens))



*------------------------------------------------------------*
* Label variables
*------------------------------------------------------------*
label var ggap_16      "Gender turnout gap (female–male, %) — age 16"
label var ggap_17      "Gender turnout gap (female–male, %) — age 17"
label var ggap_18      "Gender turnout gap (female–male, %) — age 18"
label var ggap_19      "Gender turnout gap (female–male, %) — age 19"
label var ggap_20      "Gender turnout gap (female–male, %) — age 20"
label var ggap_21_24   "Gender turnout gap (female–male, %) — ages 21–24"
label var ggap_25_29   "Gender turnout gap (female–male, %) — ages 25–29"
label var ggap_30_34   "Gender turnout gap (female–male, %) — ages 30–34"
label var ggap_35_39   "Gender turnout gap (female–male, %) — ages 35–39"
label var ggap_40_44   "Gender turnout gap (female–male, %) — ages 40–44"
label var ggap_45_49   "Gender turnout gap (female–male, %) — ages 45–49"
label var ggap_50_54   "Gender turnout gap (female–male, %) — ages 50–54"
label var ggap_55_59   "Gender turnout gap (female–male, %) — ages 55–59"
label var ggap_60_64   "Gender turnout gap (female–male, %) — ages 60–64"
label var ggap_65_69   "Gender turnout gap (female–male, %) — ages 65–69"
label var ggap_70_74   "Gender turnout gap (female–male, %) — ages 70–74"
label var ggap_75_79   "Gender turnout gap (female–male, %) — ages 75–79"
label var ggap_80_84   "Gender turnout gap (female–male, %) — ages 80–84"
label var ggap_85_89   "Gender turnout gap (female–male, %) — ages 85–89"
label var ggap_90_94   "Gender turnout gap (female–male, %) — ages 90–94"
label var ggap_95_99   "Gender turnout gap (female–male, %) — ages 95–99"
label var ggap_total   "Gender turnout gap (female–male, %) — total electorate"




*------------------------------------------------------------*
* Drop duplicated
*------------------------------------------------------------*


drop if rank_votos==2




* Format as a panel

xtset cd_municipio ano_eleicao


* Gen a margin variable from the last election (4 year lag)

gen lmargin = l4.margem


* Encode variables for control

encode escolaridade, gen(Education)
gen edu_degree = 0
replace edu_degree = 1 if Education==6

encode raca, gen(Race)
gen race_white = 0
replace race_white = 1 if Race==2


* Graph by model
set scheme plotplain

 
rdplot pturnout lmargin, binselect(es) ci(95) ///
       graph_options(title(Total Turnout % ()) ///
                           ytitle( ) ///
                           xtitle(Margin of Victory at time t))



rdplot  fem_turnout lmargin, binselect(es) ci(95) ///
       graph_options(title(Female Turnout %)) ///
                           ytitle( ) ///
                           xtitle(Margin of Victory at time t))

						   
rdplot mal_turnout lmargin, binselect(es) ci(95) ///
       graph_options(title(Male Turnout %)) ///
                           ytitle( ) ///
                           xtitle(Margin of Victory at time t))


						   


rdplot ggap_total   margem, binselect(es) ci(95) ///
       graph_options(title(Gender turnout gap (female–male, %) — total electorate) ///
                           ytitle(Gender Gap ) ///
                           xtitle(Margin of Victory at time t))


						   
						   
						   
						   
						   
*
*						   
*
* Se ainda não tiver:
* ssc install rdrobust, replace

* Limpa gráficos antigos
graph drop _all

* Loop principal
local groups "16 17 18 19 20 21_24 25_29 30_34 35_39 40_44 45_49 50_54 55_59 60_64 65_69 70_74 75_79 80_84 85_89 90_94 95_99 total"
local glist

foreach suf of local groups {
    local yvar = "ggap_`suf'"
    capture confirm variable `yvar'
    if _rc continue

    rdplot `yvar' lmargin, binselect(es) ci(95) ///
        graph_options(title("Gender gap — `suf'") ///
                      ytitle() ///
                      xtitle())

    * Renomeia o gráfico ativo
    graph rename g_`suf', replace

    * Adiciona à lista
    local glist `glist' g_`suf'
}

* Agora combina os gráficos (só se eles existirem)
grc1leg2  `glist', imargin(vsmall) ///
    xcommon rows(4) cols(6) title() ///
    name(panel_gaps, replace) 

	

* (Opcional) Exporta o painel
* graph export "panel_gaps.png", replace width(2400)

covs(covars)

*------------------------------------------------------------*
* Rodar UM por vez e armazenar com estimates store
*------------------------------------------------------------*

eststo clear

* Model - Total Turnout %
* =========================
* RD: Total Turnout %
* =========================
rdrobust pturnout lmargin, covs(race_white edu_degree rendimento_medio_pop ///
state1 state2 state3 state4 state5 state6 state7 state8 state9 state10 ///
state11 state12 state13 state14 state15 state16 state17 state18 state19 ///
state20 state21 state22 state23 state24 state26) all
estimates store m_pturnout

* =========================
* RD: Female Turnout %
* =========================
rdrobust fem_turnout lmargin, covs(race_white edu_degree rendimento_medio_pop ///
state1 state2 state3 state4 state5 state6 state7 state8 state9 state10 ///
state11 state12 state13 state14 state15 state16 state17 state18 state19 ///
state20 state21 state22 state23 state24 state26) all
estimates store m_fem_total

* =========================
* RD: Male Turnout %
* =========================
rdrobust mal_turnout lmargin, covs(race_white edu_degree rendimento_medio_pop ///
state1 state2 state3 state4 state5 state6 state7 state8 state9 state10 ///
state11 state12 state13 state14 state15 state16 state17 state18 state19 ///
state20 state21 state22 state23 state24 state26) all
estimates store m_mal_total

* =========================
* Tabela LaTeX (esttab)
* =========================
esttab m_pturnout m_fem_total m_mal_total ///
    using "C:\Users\Administrador\Meu Drive\00 Pesquisa\# Gender Gap Turnout\Dados e Análise\TSE Data\Results\table_total_turnout.tex", ///
    f legend label replace booktabs collabels(none) ///
    cells(b(star fmt(3) vacant({--})) se(par([ ]) fmt(3))) ///
    stats(ci_rb kernel bwsel N se_cl pv_cl pv_rb pord qord h_bw b_bw, ///
          fmt(0 0 0 0 3 3 3 0 0 3 3) ///
          layout("\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" ///
                 "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" ///
                 "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" ///
                 "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}") ///
          labels(`"Robust 95\% CI"' `"Kernel Type"' `"BW Type"' `"Observations"' ///
                 `"Conventional Std. Error"' `"Conventional p-value"' `"Robust p-value"' ///
                 `"Order Loc. Poly. (p)"' `"Order Bias (q)"' `"BW Loc. Poly. (h)"' `"BW Bias (b)"'))

 
*------------------------------------------------------------*
* Modelos da idade voluntária - estimates store
*------------------------------------------------------------*

 
 
 * --- Female Turnout 2016 ---
rdrobust fem_turnout_16 lmargin, covs(race_white edu_degree rendimento_medio_pop ///
state1 state2 state3 state4 state5 state6 state7 state8 state9 state10 ///
state11 state12 state13 state14 state15 state16 state17 state18 state19 ///
state20 state21 state22 state23 state24 state26) all
estimates store m_fem_16

* --- Male Turnout 2016 ---
rdrobust mal_turnout_16 lmargin, covs(race_white edu_degree rendimento_medio_pop ///
state1 state2 state3 state4 state5 state6 state7 state8 state9 state10 ///
state11 state12 state13 state14 state15 state16 state17 state18 state19 ///
state20 state21 state22 state23 state24 state26) all level(90)
estimates store m_mal_16

* --- Female Turnout 2017 ---
rdrobust fem_turnout_17 lmargin, covs(race_white edu_degree rendimento_medio_pop ///
state1 state2 state3 state4 state5 state6 state7 state8 state9 state10 ///
state11 state12 state13 state14 state15 state16 state17 state18 state19 ///
state20 state21 state22 state23 state24 state26) all
estimates store m_fem_17

* --- Male Turnout 2017 ---
rdrobust mal_turnout_17 lmargin, covs(race_white edu_degree rendimento_medio_pop ///
state1 state2 state3 state4 state5 state6 state7 state8 state9 state10 ///
state11 state12 state13 state14 state15 state16 state17 state18 state19 ///
state20 state21 state22 state23 state24 state26) all
estimates store m_mal_17

* --- Female Turnout 2018 ---
rdrobust fem_turnout_18 lmargin, covs(race_white edu_degree rendimento_medio_pop ///
state1 state2 state3 state4 state5 state6 state7 state8 state9 state10 ///
state11 state12 state13 state14 state15 state16 state17 state18 state19 ///
state20 state21 state22 state23 state24 state26) all
estimates store m_fem_18

* --- Male Turnout 2018 ---
rdrobust mal_turnout_18 lmargin, covs(race_white edu_degree rendimento_medio_pop ///
state1 state2 state3 state4 state5 state6 state7 state8 state9 state10 ///
state11 state12 state13 state14 state15 state16 state17 state18 state19 ///
state20 state21 state22 state23 state24 state26) all
estimates store m_mal_18

* --- Female Turnout 2019 ---
rdrobust fem_turnout_19 lmargin, covs(race_white edu_degree rendimento_medio_pop ///
state1 state2 state3 state4 state5 state6 state7 state8 state9 state10 ///
state11 state12 state13 state14 state15 state16 state17 state18 state19 ///
state20 state21 state22 state23 state24 state26) all
estimates store m_fem_19

* --- Male Turnout 2019 ---
rdrobust mal_turnout_19 lmargin, covs(race_white edu_degree rendimento_medio_pop ///
state1 state2 state3 state4 state5 state6 state7 state8 state9 state10 ///
state11 state12 state13 state14 state15 state16 state17 state18 state19 ///
state20 state21 state22 state23 state24 state26) all
estimates store m_mal_19

 
 
esttab m_fem_16 m_mal_16 m_fem_17 m_mal_17 m_fem_18 m_mal_18 m_fem_19 m_mal_19 ///
    using "C:\Users\Administrador\Meu Drive\00 Pesquisa\# Gender Gap Turnout\Dados e Análise\TSE Data\Results\table_fem_mal_turnout.tex", ///
    f legend label replace booktabs collabels(none) ///
    cells(b(star fmt(3) vacant({--})) se(par([ ]) fmt(3))) ///
    stats(ci_rb kernel bwsel N se_cl pv_cl pv_rb pord qord h_bw b_bw, ///
          fmt(0 0 0 0 3 3 3 0 0 3 3) ///
          layout("\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" ///
                 "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" ///
                 "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}") ///
          labels(`"Robust 95\% CI"' `"Kernel Type"' `"BW Type"' `"Observations"' ///
                 `"Conventional Std. Error"' `"Conventional p-value"' `"Robust p-value"' ///
                 `"Order Loc. Poly. (p)"' `"Order Bias (q)"' `"BW Loc. Poly. (h)"' `"BW Bias (b)"'))
 
 
esttab m_fem_16 m_mal_16 m_fem_17 m_mal_17 m_fem_18 m_mal_18 m_fem_19 m_mal_19 ///
    using "C:\Users\Administrador\Meu Drive\00 Pesquisa\# Gender Gap Turnout\Dados e Análise\TSE Data\Results\table_fem_mal_turnout.rtf", ///
    rtf replace legend label collabels(none) ///
    cells(b(star fmt(3) vacant({--})) se(par([ ]) fmt(3))) ///
    stats(ci_rb kernel bwsel N se_cl pv_cl pv_rb pord qord h_bw b_bw, ///
          fmt(0 0 0 0 3 3 3 0 0 3 3) ///
          labels(`"Robust 95% CI"' `"Kernel Type"' `"BW Type"' `"Observations"' ///
                 `"Conventional Std. Error"' `"Conventional p-value"' `"Robust p-value"' ///
                 `"Order Loc. Poly. (p)"' `"Order Bias (q)"' `"BW Loc. Poly. (h)"' `"BW Bias (b)"'))
 
 
 
 
 
 






rdrobust ggap_total lmargin, all
estimates store m_ggap_total

rdrobust ggap_16 lmargin, all
estimates store m_ggap_16

rdrobust ggap_17 lmargin, all
estimates store m_ggap_17

rdrobust ggap_18 lmargin, all
estimates store m_ggap_18

rdrobust ggap_19 lmargin, all
estimates store m_ggap_19

* (opcional) Dar nomes amigáveis às colunas no esttab:
esttab m_pturnout m_ggap_total m_ggap_16 m_ggap_17 m_ggap_18 m_ggap_19, ///
    mlabels("pturnout" "ggap_total" "ggap_16" "ggap_17" "ggap_18" "ggap_19")

*------------------------------------------------------------*
* Exportar a tabela LaTeX no formato do exemplo
*------------------------------------------------------------*
esttab m_pturnout m_ggap_total m_ggap_16 m_ggap_17 m_ggap_18 m_ggap_19 ///
    using "C:\Users\Administrador\Meu Drive\00 Pesquisa\# Gender Gap Turnout\Dados e Análise\TSE Data\Results\table1.tex", ///
    f legend label replace booktabs collabels(none) ///
    cells(b(star fmt(3) vacant({--})) se(par([ ]) fmt(3))) ///
    stats(ci_rb kernel bwsel N se_cl pv_cl pv_rb pord qord h_bw b_bw, ///
          fmt(0 0 0 0 3 3 3 0 0 3 3) ///
          layout("\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" ///
                 "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" ///
                 "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}") ///
          labels(`"Robust 95\% CI"' `"Kernel Type"' `"BW Type"' `"Observations"' ///
                 `"Conventional Std. Error"' `"Conventional p-value"' `"Robust p-value"' ///
                 `"Order Loc. Poly. (p)"' `"Order Bias (q)"' `"BW Loc. Poly. (h)"' `"BW Bias (b)"'))

						   
						   
esttab m_pturnout m_ggap_total m_ggap_16 m_ggap_17 m_ggap_18 m_ggap_19 ///
    using "C:\Users\Administrador\Meu Drive\00 Pesquisa\# Gender Gap Turnout\Dados e Análise\TSE Data\Results\table1.rtf", ///
    rtf replace legend label collabels(none) ///
    cells(b(star fmt(3) vacant({--})) se(par([ ]) fmt(3))) ///
    stats(ci_rb kernel bwsel N se_cl pv_cl pv_rb pord qord h_bw b_bw, ///
          fmt(0 0 0 0 3 3 3 0 0 3 3) ///
          labels(`"Robust 95% CI"' `"Kernel Type"' `"BW Type"' `"Observations"' ///
                 `"Conventional Std. Error"' `"Conventional p-value"' `"Robust p-value"' ///
                 `"Order Loc. Poly. (p)"' `"Order Bias (q)"' `"BW Loc. Poly. (h)"' `"BW Bias (b)"'))


						   
						   
